package com.example.obs.controller;


import com.example.obs.model.domin.ObsFile;
import com.example.obs.model.domin.Share;
import com.example.obs.model.domin.User;
import com.example.obs.model.utils.ObsPage;
import com.example.obs.service.FlileService.FileService;
import com.example.obs.service.ShareService.ShareService;
import com.example.obs.utils.JsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Api(tags = "分享接口")
@RequestMapping("/shares")
@RestController
public class SharesController {
    @Autowired
    private ShareService shareService;
    @Autowired
    private FileService fileService;


    @ApiOperation(value = "获得分享列表")
    @RequestMapping(value ="/selectSharesList",method = RequestMethod.POST)
    public ObsPage selectSharesList(@RequestBody User user){


        return new ObsPage();
    }
    @ApiOperation(value = "添加分享")
    @RequestMapping(value = "/addShare",method = RequestMethod.POST)
    public Share addShare(@RequestBody Map<String,Object> data) throws Exception {
        Share share = JsonUtils.map2obj((Map<String,Object>) data.get("Share"),Share.class);
        Integer validPeriod = (Integer) data.get("validPeriod");

        String uuid = UUID.randomUUID().toString();
        String shareId = uuid.substring(0, 8) + uuid.substring(9, 13) + uuid.substring(14, 18) + uuid.substring(19, 23) + uuid.substring(24);
        Date shareTime =new Date();
        if (validPeriod!=null){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(shareTime);
            calendar.add(Calendar.DATE,validPeriod);
            Date endTime = calendar.getTime();
            share.setEndTime(endTime);
        }
        share.setShareId(shareId);
        share.setShareTime(shareTime);
        shareService.addShare(share);
        return share;
    }
    @ApiOperation(value = "取消分享")
    @RequestMapping(value ="/removeShare",method = RequestMethod.POST)
    public int removeShare(@RequestBody String shareId){
        shareService.removeShare(shareId);
        return 1;
    }
    @ApiOperation(value = "浏览分享")
    @RequestMapping(value="/browseShare",method = RequestMethod.POST)
    public int browseShare(@RequestBody String shareId){
        Share share = shareService.selectShare(shareId);
        ObsFile obsFile=fileService.selectFileByFileId(share.getFileId());
        return 1;
    }
}
